# -*- coding: utf-8 -*-
# @project: huawang-api20230914
# @file： tmp_lg_07.py
# @Time：2023/9/26 14:21
# @Author：liaogan
from time import sleep

import ddddocr
from selenium import webdriver

def yk_lgin():

    driver = webdriver.Chrome()
    driver.maximize_window()
    driver.implicitly_wait(10)
    driver.get("https://jky.huawangglobal.com/admin/#/login")

    ele_code = driver.find_element('xpath', '//div/img')

    with open('code.png', 'wb') as f:
        f.write(ele_code.screenshot_as_png)  # 写入到一个图片
    # 初始化1个ocr对象
    ocr = ddddocr.DdddOcr(show_ad=False)
    # 获取当前验证码
    code = ocr.classification(ele_code.screenshot_as_png)
    print("验证码识别结果：",code)
    print(code[0],code[1],code[2])
    count = 0
    num = 5
    for one in range(num):
        if code[1] == "+":
            res = int(code[0]) + int(code[2])
        elif code[1] == "-":
            res = int(code[0]) - int(code[2])
        elif code[1] == "x":
            res = int(code[0]) * int(code[2])
        else:
            res = 123456

        username = driver.find_element('xpath', '//*[@placeholder="账号"]')
        password = driver.find_element('xpath', '//*[@placeholder="密码"]')
        yzm = driver.find_element('xpath', '//*[@placeholder="验证码"]')

        """先清理再输入密码"""
        username.clear()
        password.clear()
        yzm.clear()
        username.send_keys("autotest")
        password.send_keys("autotest123.")
        yzm.send_keys(res)
        driver.find_element("css selector", '.el-button--medium').click()
        sleep(3)

        success_url = driver.current_url
        print(success_url)
        count += 1
        if '/#/dashboard' in success_url:
            print('登录成功')
            sleep(2)
            break
        elif count == num:
            print(f'尝试了{num}次获取验证码仍然失败，请重新检查复杂度！')
            driver.close()

    return driver
if __name__ == '__main__':
    yk_lgin()




